<template>
  <div class="zeng">
    <!-- 顶部 -->
    <van-nav-bar left-text="新增地址" left-arrow @click-left="$router.go(-1)" />
    <div class="newress">
      <div class="lian">
        <p>联系人</p>
        <div class="xuan">
          <div class="inputbox">
            <input type="text" placeholder="姓名" v-model="name" />
          </div>
          <div class="sex">
            <button :class="{ btne: num == index }" v-for="(item, index) in sexarr" :key="index" @click="sexClick(index)">
              {{ item }}
            </button>
          </div>
        </div>
      </div>
      <div class="dian">
        <p>电话</p>
        <input type="text" placeholder="手机号码" v-model="phone" />
        <span>+通讯录</span>
      </div>
      <div class="shou">
        <p>地址</p>
        <input type="text" placeholder="选择收货地址" v-model="address" />
      </div>
      <div class="shou">
        <p>门牌号</p>
        <input type="text" placeholder="例:5号楼203室" />
      </div>
      <div class="qian">
        <p>标签</p>
        <div class="btn">
          <button :class="{ active: sun == index }" v-for="(item, index) in homearr" :key="index" @click="homeClick(index)">
            {{ item }}
          </button>
        </div>
      </div>
      <button @click='push'> 保存并使用</button>
    </div>
  </div>
</template>

<script>
import { xinzeng_address } from "../axios/api";
import { Toast } from 'vant';
import { Dialog } from 'vant';

export default {
  data() {
    return {
      sexarr: ["男", "女"],
      homearr: ["家", "学校", "公司"],
      num: null,
      sun: null,
      address: "",
      phone: "",
      name: "",
    };
  },
  methods: {
    sexClick(index) {
      this.num = index;
    },
    homeClick(index) {
      this.sun = index;
    },
    fun() {
      this.$router.go(-1);
    },
    push() {
      let username = JSON.parse(localStorage.getItem('userinfo')).id

      xinzeng_address({ userid: username, address: this.address, phone: this.phone, name: this.name }).then(
        response => {
          if (response.data.code == 200) {
            Toast.clear();
            this.phone = ''
            this.address = ''
            this.name = ''

            Dialog.confirm({
              title: '添加地址',
              message: '添加成功，是否前往查看？',
            })
              .then(() => {
                this.$router.push('/myaddress')
              })
              .catch(() => {
                // this.$router.go(-1)
              });
          }
        }
      )
    }

  },
};
</script>

<style lang="scss" scoped>
.newress {
  padding: 0.625rem;
  .lian {
    display: flex;
    justify-content: space-between;
    p {
      font-weight: bold;
      width: 6.25rem;
      color: #666;
    }
    .xuan {
      flex: 1;
      display: flex;
      flex-direction: column;
      .inputbox {
        border-bottom: 1px solid #f1f1f1;
        input {
          margin-bottom: 0.625rem;
          border: none;
          outline: none;
        }
      }
      .sex {
        border-bottom: 1px solid #f1f1f1;
        button {
          margin: 0.375rem 1rem 0.375rem 0;
          background-color: #fff;
          width: 3.75rem;
          height: 1.5rem;
          border: 1px solid #999;
          font-size: 0.875rem;
          color: #666;
        }
        .btne {
          color: #fff;
          background-color: #00a6ff;
          border: #00a6ff;
        }
      }
    }
  }
  .btne {
    color: #fff;
    background-color: #00a6ff;
    border: #00a6ff;
  }
  .dian {
    width: 100%;
    height: 3.125rem;
    border-bottom: 1px solid #f1f1f1;
    display: flex;
    justify-content: space-between;
    align-items: center;
    p {
      font-weight: bold;
      width: 6.25rem;
      color: #666;
    }
    input {
      border: none;
      outline: none;
    }
    span {
      color: #00a6ff;
    }
  }
  .shou {
    width: 100%;
    height: 3.125rem;
    border-bottom: 1px solid #f1f1f1;
    display: flex;
    // justify-content: space-between;
    align-items: center;
    p {
      font-weight: bold;
      width: 6.25rem;
      color: #666;
    }
    input {
      border: none;
      outline: none;
    }
  }
  .qian {
    border-bottom: 1px solid #f1f1f1;
    display: flex;
    align-items: center;
    p {
      font-weight: bold;
      width: 6.25rem;
      color: #666;
    }
    .btn {
      button {
        margin: 0.375rem 1rem 0.375rem 0;
        background-color: #fff;
        width: 3.75rem;
        height: 1.5rem;
        border: 1px solid #999;
        font-size: 0.875rem;
        color: #666;
      }
      .active {
        color: #fff;
        background-color: #00a6ff;
        border: #00a6ff;
      }
    }
  }
  > button {
    margin: 1.25rem 0;
    width: 100%;
    height: 2.5rem;
    line-height: 2.5rem;
    text-align: center;
    border: none;
    background-color: #00a6ff;
    color: #fff;
    border-radius: 0.25rem;
  }
}
</style>